package com.ymsoft.myerp.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author: yzr
 * @date: 2020/4/12
 */
public class UserDetail implements UserDetails {

    private Long userId;
    private String username;
    private String password;
    private Role role;
    private LocalDateTime createdDate;

    public UserDetail(Long userId, String username, String password, Role role, LocalDateTime createdDate) {
        setUserId(userId);
        setUsername(username);
        setPassword(password);
        setRole(role);
        setCreatedDate(createdDate);
    }

    public UserDetail(Long userId, String username, String password, Role role) {
        setUserId(userId);
        setUsername(username);
        setPassword(password);
        setRole(role);
    }

    public UserDetail(String username, String password, Role role) {
        setUsername(username);
        setPassword(password);
        setRole(role);
    }

    public UserDetail(String username, String password) {
        setUsername(username);
        setPassword(password);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(role.getName()));
        return authorities;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    /**
     * 账户是否未过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     *  账户是否未锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否未过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否激活
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
